package com.sz.biz.common.base.dto;

import com.sz.biz.common.CommonAreaEntity;
import com.sz.biz.logistics.constants.RegionConstants;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Function: 国家地区 <br>
 * Author: Charles. <br>
 * Date: 2017-04-13 17:33:00.
 */
public class CommonAreaList extends ArrayList<CountryArea> {

    public void add(CommonAreaEntity item) {
        if (item != null) {
            switch (item.getType()) {
                case 1://国家
                    this.add(new CountryArea(1, item.getCountry(), null, null));
                    break;
                case 2://中国地区
                    this.add(new CountryArea(2, item.getCountry(), item.getAreaId(), null));
                    break;
                case 3://国外地区
                    this.add(new CountryArea(3, item.getCountry(), item.getAreaId(), null));
                    break;
                default:
                    break;
            }
        }
    }

    public CommonAreaList() {

    }

    /**
     * @param list
     */
    public CommonAreaList(List<? extends CommonAreaEntity> list) {
        this.addAll(list);
    }

    public void addAll(List<? extends CommonAreaEntity> list) {
        if (list != null && list.size() > 0) {
            list.forEach(item -> this.add(item));
        }
    }

    /**
     * 获取中国地区（2）
     *
     * @return
     */
    public List<String> findCnAreas() {
        List<String> cnAreaList = new ArrayList<>();
        this.forEach(countryArea -> {
            if (countryArea.getType() == RegionConstants.Type.CHINA_REGION) {
                cnAreaList.add(countryArea.getAreaId());
            }
        });

        return cnAreaList;
    }

    public List<String> findOutCnAreas() {
        List<String> outCnAreaList = new ArrayList<>();
        this.forEach(countryArea -> {
            if (countryArea.getType() == RegionConstants.Type.FOREIGN_AREA) {
                outCnAreaList.add(countryArea.getAreaId());
            }
        });
        return outCnAreaList;
    }

    /**
     * @param separator
     * @return
     */
    public String joinName(String separator) {
        if (!StringUtils.hasLength(separator)) {
            separator = ";";
        }
        StringBuilder sBuilder = new StringBuilder();
        for (CountryArea area : this) {
            sBuilder.append(area.getName());
            sBuilder.append(separator);
        }
        if (sBuilder.length() > 0) {
            sBuilder.deleteCharAt(sBuilder.length() - 1);
        }
        return sBuilder.toString();
    }
}
